package counter.com.baomidou.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import counter.com.baomidou.entity.Serverinfo;
import counter.com.baomidou.service.IServerinfoService;
import counter.com.common.Result;
import counter.com.connproperty.Server.ServerChangeProperty;
import counter.com.connproperty.Server.ServerDetailProperty;
import counter.com.connproperty.Server.ServerNoteProperty;
import counter.com.connproperty.Server.ServerSearchProperty;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/serverinfo")
public class ServerinfoController {

    private static final Integer DATABASE_STATUS_ONLINE = 1;
    private static final Integer DATABASE_STATUS_OFFLINE = 0;

    private static final Integer SERVER_STATUS_ONLINE = 1;
    private static final Integer SERVER_STATUS_OFFLINE = 0;

    @Autowired
    IServerinfoService serverinfoService;

    @PostMapping("/search")
    public Result search(@RequestBody ServerSearchProperty data) {
        LambdaQueryWrapper<Serverinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(true, Serverinfo::getId);
//        if (!(data.getServerName().equals("") && data.getServerIp().equals(""))) {
//            wrapper.eq(Serverinfo::getName, data.getServerName()).or().eq(Serverinfo::getIp, data.getServerIp());
//        }
        String serverName = data.getServerName();
        String serverIp = data.getServerIp();
        if (serverName != null && !serverName.equals("")) {
            System.out.println("DEBUG " + serverName);
            wrapper.like(Serverinfo::getName, serverName);
        }
        if (serverIp != null && !serverIp.equals("")) {
            wrapper.like(Serverinfo::getIp, serverIp);
        }
        Page<Serverinfo> page = new Page(data.getPage(), data.getPagesize());
        IPage<Serverinfo> iPage = serverinfoService.page(page, wrapper);

        return Result.success(iPage);
    }

    @PostMapping("/detail")
    public Result detail(@RequestBody ServerDetailProperty data) {
        Serverinfo serverinfo = serverinfoService.getById(data.getId());
        if (serverinfo == null) {
            return Result.fail("未找到合适的信息");
        }
        return Result.success(serverinfo);
    }

    @PostMapping("/change")
    public Result change(@RequestBody ServerChangeProperty data) {
        Serverinfo serverinfo = serverinfoService.getById(data.getId());
        if (serverinfo == null) {
            return Result.fail("失败");
        }
        serverinfo.setServerstatus(data.getServerstatus());
        if (serverinfoService.updateById(serverinfo)) {
            return Result.success("成功");
        } else {
            return Result.fail("失败");
        }
    }

    @PostMapping("/note")
    public Result note(@RequestBody ServerNoteProperty data) {
        Serverinfo serverinfo = serverinfoService.getById(data.getId());
        if (serverinfo == null) {
            return Result.fail("服务器信息丢失");
        }
        serverinfo.setNote(data.getNote());
        if (serverinfoService.updateById(serverinfo)) {
            return Result.success("成功保存备注");
        } else {
            return Result.fail("保存备注失败");
        }
    }

    private String createGuid() {
        String uid1 = RandomStringUtils.randomAlphanumeric(8) + "-" + RandomStringUtils.randomAlphanumeric(4);
        String uid2 = RandomStringUtils.randomAlphanumeric(4) + "-" + RandomStringUtils.randomAlphanumeric(4);
        String uid3 = RandomStringUtils.randomAlphanumeric(12);
        String guid = uid1 + "-" + uid2 + "-" + uid3;
        return guid;
    }
}
